package cn.bdqn.t354.controller;

import cn.bdqn.t354.pojo.User;
import cn.bdqn.t354.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Resource
    UserService userService;
    /**
     * 跳转登录页面
     * @return
     */
    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    /**
     * 跳转主页面
     * @return
     */
    @RequestMapping("/main")
    public String main(){
        return "main";
    }

    @RequestMapping("/dologin")
    public String dologin(String usrName, String usrPassword,
                          HttpSession session, Model model){
        User user = userService.login(usrName, usrPassword);
        if(user!=null){
            session.setAttribute("loginUser",user);
            return "redirect:/main";
        }else{
            model.addAttribute("msg","用户名或者密码错误");
            return "forward:/login";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("loginUser");
        return "forward:/login";
    }
}
